//
//  KZNearByController.m
//  xiaorizi
//
//  Created by kun on 15/10/31.
//  Copyright © 2015年 origin. All rights reserved.
//

#import "KZNearByController.h"
#import "KZGlobalApi.h"

//添加本地定位
#import <CoreLocation/CoreLocation.h>

//添加地图map
#import <MapKit/MapKit.h>
#import "UIViewAdditions.h"
#import "UserMapView.h"


// 实现一个地图的代理方法
@interface KZNearByController ()<CLLocationManagerDelegate>
//用户位置定位
@property (nonatomic,strong) CLLocationManager *locationManger;


//@property (weak, nonatomic) IBOutlet UIBarButtonItem *swipMapButoon;


@property (nonatomic,weak)UserMapView *useMapView;




@end

@implementation KZNearByController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    //开启本地定位
    [self createLocationManger];
    
    //添加地图地图跳转界面
    
    //异步加载地图
    //创建异步队列
    


}

//2 添加个人位置定位Map
-(void) createMapKit
{
    
    UserMapView *view = [UserMapView userMapView];
    view.frame = CGRectMake(0, 0, self.view.width, self.view.height - 64);
                    [self.tableView addSubview:view];
                    _useMapView = view;
                    _useMapView.hidden = YES;
                    _useMapView.dataArray = self.data;

    //创建view
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        UserMapView *view = [UserMapView userMapView];
//        
//        if(view)
//        {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                
//                view.frame = CGRectMake(0, 0, self.view.width, self.view.height - 64);
//                [self.tableView addSubview:view];
//                _useMapView = view;
//                _useMapView.hidden = YES;
//                _useMapView.dataArray = self.data;
//            });
//        }
//    });
}


//1 配置本地定位
-(void)createLocationManger
{
    //查看是否打开地图定位
    if(![CLLocationManager locationServicesEnabled])
    {
        NSLog(@"Location Service Not Enabled");
        return;
    }
    self.locationManger = [CLLocationManager new];
    
    //设置定位精确度 移动多少时候定位更新
    //self.locationManger.distanceFilter = 10;
    
    if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
    {
        [self.locationManger requestWhenInUseAuthorization];
    }
    
    self.locationManger.delegate = self;
    
    //开启定位
    [self.locationManger startUpdatingLocation];
    
}


////打开本地定位
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    NSLog(@"---->location: %@",locations);
    [manager stopUpdatingLocation];
}


//实现地图的一个代理事件 显示用户地址

- (IBAction)clickedSwithMapKit:(id)sender {
    
    UIButton *btn = sender;
    
    btn.selected = !btn.selected;
    
    if(btn.selected)
    {
        [self animationFlipFromLeft:self.tableView];
        //_useMapView.hidden = NO;
        self.useMapView.hidden = NO;
 
    }
    else{
        [self jk:self.tableView];
        //_useMapView.hidden = YES;
        self.useMapView.hidden = YES;
    }
}


//地图懒加载

-(UserMapView *)useMapView
{
    if(_useMapView == nil)
    {
        UserMapView *view = [UserMapView userMapView];
        view.frame = CGRectMake(0, 0, self.view.width, self.view.height - 64);
        [self.tableView addSubview:view];
        _useMapView = view;
        _useMapView.hidden = YES;
        _useMapView.dataArray = self.data;
    }
    return _useMapView;
}



//用来实现图片反转
- (void)animationFlipFromLeft:(UIView *)view
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:YES];
    [UIView commitAnimations];
}
- (void)jk:(UIView *)view
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:YES];
    [UIView commitAnimations];
}

//隐藏下面tabbar
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}






//重写父类方法
- (void)downloadDataPath
{
    self.url = [KZGlobalApi nearByUrl:0];
    self.cachePath = [KZGlobalApi nearByCachePath:0];
    self.refreshType = FLRefreshTypeHeader;
    self.title = @"附近";
}




-(NSString *)JsonModelPath
{
    return [[NSBundle mainBundle]pathForResource:@"address" ofType:@"json"];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
}


@end
